package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.base.core.EmptyUtils;
import com.base.core.config.DefalutSp;
import com.base.core.net.RetrofitClient;
import com.base.core.ui.BaseCore;
import com.bun.miitmdid.core.JLibrary;
import com.bun.miitmdid.core.MdidSdkHelper;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.UUID;

import androidx.annotation.NonNull;
import cn.bmob.v3.Bmob;

public class MyApplication extends Application {
    public static String oaid = "";

    @Override
    public void onCreate() {
        super.onCreate();
        BaseCore.init(this, "https://api.bdav.tv", false);
        Bmob.initialize(this, "3065556788a41494c91ca01c0028a034");
        CrashReport.initCrashReport(getApplicationContext(), "03e4c18226", false);
        RetrofitClient.init("https://api.bdav.tv/");
        Retrofit2Client.init("https://api.bdav.tv/");
        try {
            JLibrary.InitEntry(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        MdidSdkHelper.InitSdk(this, true, new MiitHelper(new MiitHelper.AppIdsUpdater() {
            @Override
            public void OnIdsAvalid(@NonNull String ids) {
                //根方法中,我们如果只需要oaid,则只获取oaid即可
                // String oaid=_supplier.getOAID();
                oaid = ids;
                Log.d("oaid", ids);
            }
        }));
        String uniqueID = UUID.randomUUID().toString() +
                Build.BRAND;
        if (EmptyUtils.isEmpty(DefalutSp.getString("uuid"))) {
            DefalutSp.putString("uuid", uniqueID);
            return;
        } else {
            uniqueID = DefalutSp.getString("uuid");
        }

        Log.d("uuid", uniqueID);
    }

    public static String getdevicetoken(Context context) {
        if (EmptyUtils.isNotEmpty(MyApplication.oaid)) {
            return MyApplication.oaid;
        } else {
            return getAndroidId(context);
        }

    }

    public static String getAndroidId(Context context) {
        String ANDROID_ID = Settings.System.getString(context.getContentResolver(),
                Settings.System.ANDROID_ID);
        if (EmptyUtils.isEmpty(ANDROID_ID)) {
            ANDROID_ID = DefalutSp.getString("uuid");
        }
        if (EmptyUtils.isEmpty(ANDROID_ID)) {
            String s = Build.BOARD + Build.BRAND + Build.HARDWARE;
            return MD5Utils.getMD5(s);
        }
        Log.d("ANDROID_ID", ANDROID_ID);
        return ANDROID_ID;
    }

    @SuppressLint("MissingPermission")
    public static String getIMEI(Context context) {
        TelephonyManager tel = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        return tel.getDeviceId();
    }

    @SuppressLint("MissingPermission")
    public static String getSimSerialNumber(Context context) {
        TelephonyManager tm =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String simSerialNumber = tm.getSimSerialNumber();
        if (EmptyUtils.isEmpty(simSerialNumber)) {
            simSerialNumber = getIMEI(context);
        }
        Log.d("simSerialNumber", simSerialNumber);
        return simSerialNumber;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }
}
